import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.StringTokenizer;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SwingConstants;
import javax.swing.SpinnerNumberModel;

public class MainPanel {

	public static CardLayout cardLayoutTicket;
	public static Panel cardTicketPanel;
	private CardLayout cardLayoutSeat;
	JPanel ticketPanel = new JPanel();

	Suwon match1 = new SuwonVSSeoul();
	Seoul match2 = new BusanVSPohang();
	Busan match3 = new SuwonVSBusan();

	int NoOfPeople;

	JSpinner spin_noAdult;
	JSpinner spin_noChild;
	JComboBox matchcombo;

	public MainPanel() {

		cardTicketPanel = new Panel();
		cardTicketPanel.setSize(1500, 1500);

		ticketPanel.setIgnoreRepaint(true);
		ticketPanel.setSize(1500, 1500);
		cardLayoutTicket = new CardLayout();
		cardTicketPanel.setLayout(cardLayoutTicket);

		JPanel panel = new JPanel();

		panel.setSize(1500, 1500);
		cardTicketPanel.add(panel, "name_30474285056164");
		panel.setLayout(null);

		JLabel lblWelcome = new JLabel("Welcome!");
		lblWelcome.setForeground(new Color(0, 0, 255));
		lblWelcome.setFont(new Font("Minion Pro Cond", Font.BOLD, 98));
		lblWelcome.setHorizontalAlignment(SwingConstants.CENTER);
		lblWelcome.setBounds(100, 25, 571, 245);
		panel.add(lblWelcome);
		cardTicketPanel.add(panel, "welcome");
		cardTicketPanel.add(ticketPanel);

		ticketPanel.setBounds(4, 0, 422, 91);
		ticketPanel.setLayout(null);

		final JComboBox cb_CHorQUICK = new JComboBox();
		cb_CHorQUICK.setFont(new Font("Yu Mincho Demibold", Font.PLAIN, 12));
		cb_CHorQUICK.setBounds(4, 42, 106, 21);
		ticketPanel.add(cb_CHorQUICK);
		cb_CHorQUICK.setModel(new DefaultComboBoxModel(new String[] {
				"Choose Seats", "Quick Seats" }));

		matchcombo = new JComboBox();
		matchcombo.setFont(new Font("Yu Mincho Demibold", Font.PLAIN, 12));
		matchcombo.setBounds(4, 6, 239, 21);
		ticketPanel.add(matchcombo);
		matchcombo.setModel(new DefaultComboBoxModel(new String[] {
				"Suwon vs Seoul      12/14    12:00",
				"Busan vs Pohang     12/16    15:00",
				"Suwon vs Busan      12/18    13:00" }));

		final JComboBox cb_direction = new JComboBox();
		cb_direction.setFont(new Font("Yu Mincho Demibold", Font.PLAIN, 12));
		cb_direction.setBounds(158, 42, 161, 21);
		ticketPanel.add(cb_direction);
		cb_direction.setModel(new DefaultComboBoxModel(new String[] {
				"Choose Class", "A class Seat (West)", "B class Seat (East)",
				"C class Seat (North)", "D class Seat (South)" }));

		JLabel lblSdfsd = new JLabel("No. of adults");
		lblSdfsd.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 12));
		lblSdfsd.setBounds(342, 23, 97, 15);
		ticketPanel.add(lblSdfsd);

		JLabel lblNoOfChildren = new JLabel("No. of children");
		lblNoOfChildren.setFont(new Font("Tekton Pro Ext", Font.PLAIN, 12));
		lblNoOfChildren.setBounds(342, 45, 97, 15);
		ticketPanel.add(lblNoOfChildren);

		spin_noAdult = new JSpinner();
		spin_noAdult.setModel(new SpinnerNumberModel(0, 0, 9, 1));
		spin_noAdult.setBounds(451, 16, 29, 22);
		ticketPanel.add(spin_noAdult);

		spin_noChild = new JSpinner();
		spin_noChild.setModel(new SpinnerNumberModel(0, 0, 9, 1));
		spin_noChild.setBounds(451, 41, 29, 22);
		ticketPanel.add(spin_noChild);

		JButton btnNext = new JButton("NEXT");
		btnNext.setFont(new Font("Consolas", Font.BOLD, 12));
		btnNext.setBounds(578, 66, 65, 23);
		ticketPanel.add(btnNext);

		final JPanel panel_1 = new JPanel();
		panel_1.setBounds(4, 100, 600, 120);
		ticketPanel.add(panel_1);
		cardLayoutSeat = new CardLayout();
		panel_1.setLayout(cardLayoutSeat);

		JPanel panel_3 = new JPanel();
		panel_1.add(panel_3, "empty");
		final JButton btnReserve = new JButton();
		btnReserve.setFont(new Font("Consolas", Font.BOLD, 12));
		btnReserve.setBounds(578, 240, 150, 23);
		ticketPanel.add(btnReserve);
		btnReserve.setText("Ticketing");
		btnReserve.setVisible(false);

		// It writes the changes of the seats
		btnReserve.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {

				String currentMatch = null;
				String direction = null;

				try {
					FileReader currentMatchReader = new FileReader(
							"currentMatch.txt");
					BufferedReader currentMatchBufferedReader = new BufferedReader(
							currentMatchReader);

					String line = null;
					while ((line = currentMatchBufferedReader.readLine()) != null) {
						StringTokenizer st = new StringTokenizer(line, "-");
						currentMatch = st.nextToken();

						direction = st.nextToken();

					}
					// Go to first page after press
					cardLayoutSeat.show(panel_1, "empty");
					cardLayoutTicket.show(cardTicketPanel, "welcome");
					cb_CHorQUICK.setSelectedIndex(0);
					cb_direction.setSelectedIndex(0);
					spin_noAdult.setValue(0);
					spin_noChild.setValue(0);
					btnReserve.setVisible(false);

					if (matchcombo.getSelectedIndex() == 0)
						match1.findMatch(currentMatch, direction);
					else if (matchcombo.getSelectedIndex() == 1)
						match2.findMatch(currentMatch, direction);
					else if (matchcombo.getSelectedIndex() == 2)
						match3.findMatch(currentMatch, direction);
				} catch (IOException e1) {

				}
			}
		});

		// It shows the seats according to match and direction.
		btnNext.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				NoOfPeople = ((int) spin_noAdult.getValue() + (int) spin_noChild
						.getValue());
				String match = (String) matchcombo.getSelectedItem();
				String direction = (String) cb_direction.getSelectedItem();

				try {
					FileWriter howmany = new FileWriter("peoplenumber.txt");
					howmany.write(" " + NoOfPeople);
					howmany.close();
				} catch (IOException e) {
					e.printStackTrace();
				}

				try {
					FileWriter whatmatch = new FileWriter("currentMatch.txt");
					whatmatch.write(" " + match);
					whatmatch.write("-");
					whatmatch.write(direction);
					whatmatch.close();

				} catch (IOException e) {
					e.printStackTrace();
				}
				// choose seat
				if (cb_direction.getSelectedIndex() != 0
						&& cb_CHorQUICK.getSelectedIndex() == 0)
					btnReserve.setVisible(true);
				if (cb_direction.getSelectedIndex() == 0)
					JOptionPane.showMessageDialog(null,
							"Please choose the direction of seats");
				cardLayoutSeat.show(panel_1, "empty");
				if (NoOfPeople != 0) {
					// match1
					if (matchcombo.getSelectedIndex() == 0) {
						// west
						if (cb_direction.getSelectedIndex() == 1) {
							// choose seat
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match1.w_seat(), "match1 west");
								cardLayoutSeat.show(panel_1, "match1 west");
							}
							// quick seat
							if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match1.w_seat();
								match1.quickSeat(match, direction, NoOfPeople);
							}
						}
						// east
						if (cb_direction.getSelectedIndex() == 2) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match1.e_seat(), "match1 east");
								cardLayoutSeat.show(panel_1, "match1 east");
							}
							if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match1.e_seat();
								match1.quickSeat(match, direction, NoOfPeople);
							}
						}
						// north
						if (cb_direction.getSelectedIndex() == 3) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match1.n_seat(), "match1 north");
								cardLayoutSeat.show(panel_1, "match1 north");
							}
							if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match1.n_seat();
								match1.quickSeat(match, direction, NoOfPeople);
							}
						}
						// south
						if (cb_direction.getSelectedIndex() == 4) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match1.s_seat(), "match1 south");
								cardLayoutSeat.show(panel_1, "match1 south");
							}
							if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match1.s_seat();
								match1.quickSeat(match, direction, NoOfPeople);
							}
						}
					}
					// match2
					if (matchcombo.getSelectedIndex() == 1) {
						if (cb_direction.getSelectedIndex() == 1) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match2.w_seat(), "match2 west");
								cardLayoutSeat.show(panel_1, "match2 west");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match2.w_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 2) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match2.e_seat(), "match2 east");
								cardLayoutSeat.show(panel_1, "match2 east");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match2.e_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 3) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match2.n_seat(), "match2 north");
								cardLayoutSeat.show(panel_1, "match2 north");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match2.n_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 4) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match2.s_seat(), "match2 south");
								cardLayoutSeat.show(panel_1, "match2 south");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match2.s_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						}
					}
					// match3
					if (matchcombo.getSelectedIndex() == 2) {
						if (cb_direction.getSelectedIndex() == 1) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match3.w_seat(), "match3 west");
								cardLayoutSeat.show(panel_1, "match3 west");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match3.w_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 2) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match3.e_seat(), "match3 east");
								cardLayoutSeat.show(panel_1, "match3 east");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match3.w_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 3) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match3.n_seat(), "match3 north");
								cardLayoutSeat.show(panel_1, "match3 north");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match3.w_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						} else if (cb_direction.getSelectedIndex() == 4) {
							if (cb_CHorQUICK.getSelectedIndex() == 0) {
								panel_1.add(match3.s_seat(), "match3 south");
								cardLayoutSeat.show(panel_1, "match3 south");
							} else if (cb_CHorQUICK.getSelectedIndex() == 1) {
								match3.w_seat();
								match2.quickSeat(match, direction, NoOfPeople);
							}
						}
					}
				} else {
					JOptionPane.showMessageDialog(null,
							"Please give No of people");
					btnReserve.setVisible(false);
				}
			}
		});

	}

	public void setNoOfPeople(int noOfPeople) {
		NoOfPeople = noOfPeople;
	}

	public Panel mainpanel() {
		return cardTicketPanel;

	}

	public int getNoOfPeople() {
		return NoOfPeople;
	}

}
